<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
   
  <meta name="keywords" content="郑浩枫,周芳怡,咸鱼郑某的博客" />
   
  <meta name="description" content="咸鱼郑某的博客" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Vue笔记 |  岛
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/css/main.css">

  
  <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
  
  

  

<link rel="alternate" href="/atom.xml" title="岛" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-Vue笔记" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Vue笔记
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2021/Vue%E7%AC%94%E8%AE%B0/" class="article-date">
  <time datetime="2021-03-08T04:28:10.000Z" itemprop="datePublished">2021-03-08</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/Vue/">Vue</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.1k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">5分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    
    <div class="article-entry" itemprop="articleBody">
      
      

      
      <h2 id="Vue笔记"><a href="#Vue笔记" class="headerlink" title="Vue笔记"></a>Vue笔记</h2><p><code>let</code> 定义变量， <code>const</code> 定义常量</p>
<p><code>var</code>是JS的一个设计错误，用<code>var</code>定义的变量出了代码块依然能访问</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript">&#123;<br>     <span class="hljs-keyword">var</span> a = <span class="hljs-number">10</span>;<br>&#125;<br><span class="hljs-built_in">console</span>.log(a);<br></code></pre></td></tr></table></figure>

<p>而let不行</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript">&#123;<br>    <span class="hljs-keyword">let</span> a = <span class="hljs-number">10</span>;<br>&#125;<br><span class="hljs-built_in">console</span>.log(a);<br></code></pre></td></tr></table></figure>

<p><code>const</code>定义的常量不能被第二次赋值</p>
<span id="more"></span>

<h3 id="Vue基本格式"><a href="#Vue基本格式" class="headerlink" title="Vue基本格式"></a>Vue基本格式</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>    &lt;div id&#x3D;&quot;vue_det&quot;&gt;<br>        &lt;p&gt;&#123;&#123;site&#125;&#125;&lt;&#x2F;p&gt;<br>    &lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>    var vm &#x3D; new Vue(&#123;<br>        el: &#39;#vue_det&#39;,<br>        data: &#123;<br>            site: &quot;hello&quot;<br>        &#125;<br>    &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p><code>el</code> 绑定元素，<code>#</code>为ID选择器，同理，可以使用<code>.</code>类选择器和<code>div</code>标签选择器等</p>
<h3 id="vue生命周期"><a href="#vue生命周期" class="headerlink" title="vue生命周期"></a>vue生命周期</h3><h3 id="v-once"><a href="#v-once" class="headerlink" title="v-once"></a>v-once</h3><p>在标签中加上这个的时候，值只改变一次</p>
<h3 id="v-pre"><a href="#v-pre" class="headerlink" title="v-pre"></a>v-pre</h3><p>原封不动的显示出来</p>
<h3 id="v-cloak"><a href="#v-cloak" class="headerlink" title="v-cloak"></a>v-cloak</h3><p>斗篷，没啥用</p>
<h3 id="v-text"><a href="#v-text" class="headerlink" title="v-text"></a>v-text</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>    &lt;div id&#x3D;&quot;vue_det&quot;&gt;<br>        &lt;p v-text&#x3D;&quot;aaa&quot;&gt;&lt;&#x2F;p&gt;<br>    &lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>    var vm &#x3D; new Vue(&#123;<br>        el: &#39;#vue_det&#39;,<br>        data: &#123;<br>            aaa: &quot;你好&quot;<br>        &#125;<br>    &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p><code>aaa</code>对应的值会显示在<code>v-text</code>属性为<code>aaa</code>的标签中</p>
<h3 id="v-html"><a href="#v-html" class="headerlink" title="v-html"></a>v-html</h3><p>设置元素的innerHTML</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>    &lt;div id&#x3D;&quot;vue_det&quot;&gt;<br>        &lt;p v-html&#x3D;&quot;aaa&quot;&gt;&lt;&#x2F;p&gt;<br>    &lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>    var vm &#x3D; new Vue(&#123;<br>        el: &#39;#vue_det&#39;,<br>        data: &#123;<br>            aaa: &quot;&lt;a href&#x3D;&#39;&#39;#&#39;&gt;这是测试的HTML&lt;&#x2F;a&gt;&quot;<br>        &#125;<br>    &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p><code>aaa</code>对应的HTML值，会被解析后显示在<code>v-html</code>属性为<code>aaa</code>的标签中</p>
<h3 id="v-on"><a href="#v-on" class="headerlink" title="v-on"></a>v-on</h3><p>为元素绑定事件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>    &lt;div id&#x3D;&quot;vue_det&quot;&gt;<br>		&lt;button v-on:click&#x3D;&quot;bbb&quot;&gt;&#123;&#123;msg&#125;&#125;&lt;&#x2F;button&gt;<br>		&lt;button @click&#x3D;&quot;ccc()&quot;&gt;&#123;&#123;msg&#125;&#125;&lt;&#x2F;button&gt;<br>    &lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>    var vm &#x3D; new Vue(&#123;<br>        el: &#39;#vue_det&#39;,<br>        data: &#123;<br>			msg:&quot;点我&quot;<br>        &#125;,<br>		methods:&#123;<br>			bbb:function()&#123;<br>				alert(&quot;bbb&quot;);<br>			&#125;,<br>			ccc:function()&#123;<br>				alert(&quot;ccc&quot;);<br>			&#125;<br>		&#125;<br>    &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p>使用<code>v-click</code>或<code>@click</code>均可绑定事件</p>
<h3 id="实时改变值"><a href="#实时改变值" class="headerlink" title="实时改变值"></a>实时改变值</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>    &lt;div id&#x3D;&quot;vue_det&quot;&gt;<br>		&lt;div id&#x3D;&quot;vue_det&quot;&gt;&#123;&#123;msg&#125;&#125;&lt;&#x2F;div&gt;<br>		&lt;button @click&#x3D;&quot;aaa&quot;&gt;&#123;&#123;btn&#125;&#125;&lt;&#x2F;button&gt;<br>    &lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>    var vm &#x3D; new Vue(&#123;<br>        el: &#39;#vue_det&#39;,<br>        data: &#123;<br>			btn:&quot;点击后改变值&quot;,<br>			msg:&quot;要被改变的&quot;<br>        &#125;,<br>		methods:&#123;<br>			aaa:function()&#123;<br>				this.msg &#x3D; &quot;这是改变后的&quot;<br>			&#125;<br>		&#125;<br>    &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>



<h3 id="一个计数器"><a href="#一个计数器" class="headerlink" title="一个计数器"></a>一个计数器</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>    &lt;div id&#x3D;&quot;vue_det&quot;&gt;<br>		&lt;button @click&#x3D;&quot;add()&quot;&gt;&#123;&#123;btn1&#125;&#125;&lt;&#x2F;button&gt;<br>		&lt;div&gt;&#123;&#123;num&#125;&#125;&lt;&#x2F;div&gt;<br>		&lt;button @click&#x3D;&quot;sub()&quot;&gt;&#123;&#123;btn2&#125;&#125;&lt;&#x2F;button&gt;<br>    &lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>    var vm &#x3D; new Vue(&#123;<br>        el: &#39;#vue_det&#39;,<br>        data: &#123;<br>			btn1:&quot;sub&quot;,<br>			btn2:&quot;add&quot;,<br>			num:1<br>        &#125;,<br>		methods:&#123;<br>			add:function()&#123;<br>				this.num--;<br>			&#125;,<br>			sub:function()&#123;<br>				this.num++;<br>			&#125;<br>		&#125;<br>    &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<h3 id="v-show"><a href="#v-show" class="headerlink" title="v-show"></a>v-show</h3><p>显示和隐藏元素</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>    &lt;div id&#x3D;&quot;vue_det&quot;&gt;<br>			&lt;div v-show&#x3D;&quot;flg&quot;&gt;这是文字&lt;&#x2F;div&gt;<br>			&lt;button @click&#x3D;&quot;show()&quot;&gt;&#123;&#123;btn1&#125;&#125;&lt;&#x2F;button&gt;<br>			&lt;button @click&#x3D;&quot;hide()&quot;&gt;&#123;&#123;btn2&#125;&#125;&lt;&#x2F;button&gt;<br>    &lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>	var app &#x3D; new Vue(&#123;<br>		el:&quot;#vue_det&quot;,<br>		data:&#123;<br>			btn1:&quot;显示&quot;,<br>			btn2:&quot;隐藏&quot;,<br>			flg:true<br>		&#125;,<br>		methods:&#123;<br>			show:function()&#123;<br>				this.flg &#x3D; true;<br>			&#125;,<br>			hide:function()&#123;<br>				this.flg &#x3D; false;<br>			&#125;<br>		&#125;<br>	&#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<ul>
<li>原理是修改元素的display，实现显示隐藏</li>
<li>指令后面的内容最终都会解析成布尔类型</li>
<li>数据改变之后，对应元素的显示状态会同步跟新</li>
</ul>
<h3 id="v-if"><a href="#v-if" class="headerlink" title="v-if"></a>v-if</h3><p>根据表达式的真假，切换元素的显示和隐藏</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>	&lt;div id&#x3D;&quot;app&quot;&gt;<br>		&lt;button @click&#x3D;&quot;showOrHide&quot;&gt;&#123;&#123;msg&#125;&#125;&lt;&#x2F;button&gt;<br>		&lt;p v-show&#x3D;&quot;isShow&quot;&gt;文字文字文字&lt;&#x2F;p&gt;<br>	&lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>	var app &#x3D; new Vue(&#123;<br>		el:&quot;#app&quot;,<br>		data:&#123;<br>			msg:&quot;显示或隐藏&quot;,<br>			isShow:false<br>		&#125;,<br>		methods:&#123;<br>			showOrHide:function()&#123;<br>				this.isShow &#x3D; ! this.isShow;<br>			&#125;<br>		&#125;<br>	&#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p>和<code>v-show</code>相比，<code>v-if</code>是直接操作DOM树</p>
<p><code>v-if</code>中也可以写表达式</p>
<p><code>v-show</code>对资源消耗小</p>
<h3 id="v-bind"><a href="#v-bind" class="headerlink" title="v-bind"></a>v-bind</h3><p>使用动态的值</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs "">&lt;img v-bind:src&#x3D;&quot;变量&quot;&gt;<br></code></pre></td></tr></table></figure>



<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>	&lt;div id&#x3D;&quot;app&quot;&gt;<br>		&lt;p v-bind:style&#x3D;&quot;bg&quot;&gt;文字文字文字&lt;&#x2F;p&gt;<br>	&lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>	var app &#x3D; new Vue(&#123;<br>		el:&quot;#app&quot;,<br>		data:&#123;<br>			bg:&quot;background:red&quot;<br>		&#125;<br>	&#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p>其中<code>v-bind</code>可以简写为<code>:</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>	&lt;div id&#x3D;&quot;app&quot;&gt;<br>		&lt;img :src&#x3D;&quot;imgSrc&quot; :title&#x3D;&quot;imgTitle&quot;&gt;<br>	&lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>	var app &#x3D; new Vue(&#123;<br>		el:&quot;#app&quot;,<br>		data:&#123;<br>			imgSrc:&quot;https:&#x2F;&#x2F;model-1253780623.cos.ap-guangzhou.myqcloud.com&#x2F;live2d&#x2F;model&#x2F;rem&#x2F;remu2048&#x2F;texture_00.png&quot;,<br>			imgTitle:&quot;衣服贴图&quot;<br>		&#125;<br>	&#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<h4 id="使用两种方法来判断是否应用样式"><a href="#使用两种方法来判断是否应用样式" class="headerlink" title="使用两种方法来判断是否应用样式"></a>使用两种方法来判断是否应用样式</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>	&lt;style&gt;<br>		.active&#123;<br>			border:  1px solid red;<br>		&#125;<br>	&lt;&#x2F;style&gt;<br>	&lt;div id&#x3D;&quot;app&quot;&gt;<br>		&lt;img :src&#x3D;&quot;imgSrc&quot; :title&#x3D;&quot;imgTitle +&#39;!!!&#39;&quot; :class&#x3D;&quot;isAcitve?&#39;active&#39;:&#39;&#39;&quot;&gt;<br>		&lt;img :src&#x3D;&quot;imgSrc&quot; :title&#x3D;&quot;imgTitle +&#39;!!!&#39;&quot; :class&#x3D;&quot;&#123;active:isAcitve&#125;&quot;&gt;<br>	&lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>	var app &#x3D; new Vue(&#123;<br>		el:&quot;#app&quot;,<br>		data:&#123;<br>			imgSrc:&quot;https:&#x2F;&#x2F;model-1253780623.cos.ap-guangzhou.myqcloud.com&#x2F;live2d&#x2F;model&#x2F;rem&#x2F;remu2048&#x2F;texture_00.png&quot;,<br>			imgTitle:&quot;衣服贴图&quot;,<br>			isAcitve:false<br>		&#125;<br>	&#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>
<p>其中<code>isAcitve?&#39;active&#39;:&#39;&#39;</code>为三元运算符号</p>
<p><code>&#123;active:isAcitve&#125;</code>表示，<code>active</code>是否有效，取决于<code>isAcitve</code>为真或假（推荐）</p>
<h3 id="Vue图片切换效果"><a href="#Vue图片切换效果" class="headerlink" title="Vue图片切换效果"></a>Vue图片切换效果</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>	&lt;div id&#x3D;&quot;app&quot;&gt;<br>		&lt;img :src&#x3D;&quot;imgSrcList[imgIndex]&quot; width&#x3D;&quot;500px&quot;&gt;<br>		&lt;button @click&#x3D;&quot;lastImgFun&quot; v-show&#x3D;&quot;imgIndex&gt;0&quot;&gt;&#123;&#123;lastImg&#125;&#125;&lt;&#x2F;button&gt;<br>		&lt;button @click&#x3D;&quot;nextImgFun&quot; v-show&#x3D;&quot;imgIndex&lt;imgSrcList.length-1&quot;&gt;&#123;&#123;nextImg&#125;&#125;&lt;&#x2F;button&gt;<br>	&lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>	var app &#x3D; new Vue(&#123;<br>		el:&quot;#app&quot;,<br>		data:&#123;<br>			nextImg:&quot;下一张&quot;,<br>			lastImg:&quot;上一张&quot;,<br>			imgSrcList:[<br>				&#39;https:&#x2F;&#x2F;zhfhz.gitee.io&#x2F;images&#x2F;%E6%91%84%E5%BD%B1&#x2F;DSC_0044.jpg&#39;,<br>				&#39;https:&#x2F;&#x2F;zhfhz.gitee.io&#x2F;images&#x2F;%E6%91%84%E5%BD%B1&#x2F;DSC_0045.jpg&#39;,<br>				&#39;https:&#x2F;&#x2F;zhfhz.gitee.io&#x2F;images&#x2F;%E6%91%84%E5%BD%B1&#x2F;DSC_0039_1613799292227_88.jpg&#39;,<br>				&#39;https:&#x2F;&#x2F;zhfhz.gitee.io&#x2F;images&#x2F;%E6%91%84%E5%BD%B1&#x2F;DSC_0062_1614511056097_10.jpg&#39;,<br>				&#39;https:&#x2F;&#x2F;zhfhz.gitee.io&#x2F;images&#x2F;%E6%91%84%E5%BD%B1&#x2F;DSC_0058_1614511054295_12.jpg&#39;<br>			],<br>			imgIndex:0<br>		&#125;,<br>		methods:&#123;<br>			lastImgFun:function()&#123;<br>				this.imgIndex--<br>			&#125;,<br>			nextImgFun:function()&#123;<br>				this.imgIndex++<br>			&#125;<br>		&#125;<br>	&#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<h3 id="v-for"><a href="#v-for" class="headerlink" title="v-for"></a>v-for</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>	&lt;div id&#x3D;&quot;app&quot;&gt;<br>		&lt;ul&gt;<br>			&lt;li v-for&#x3D;&quot;(item,index) in list&quot; :title&#x3D;&quot;item&quot;&gt;<br>				&#123;&#123;index&#125;&#125;--&#123;&#123;item&#125;&#125;<br>			&lt;&#x2F;li&gt;<br>		&lt;&#x2F;ul&gt;<br>		&lt;button @click&#x3D;&quot;add&quot;&gt;添加&lt;&#x2F;button&gt;<br>		&lt;button @click&#x3D;&quot;del&quot;&gt;移除&lt;&#x2F;button&gt;<br>	&lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>	var app &#x3D; new Vue(&#123;<br>		el:&quot;#app&quot;,<br>		data:&#123;<br>			list :[1,2,3,4,5]<br>		&#125;,<br>		methods:&#123;<br>			add: function()&#123;<br>				this.list.push(&quot;添加一个&quot;);<br>			&#125;,<br>			del:function()&#123;<br>				this.list.shift(); &#x2F;&#x2F;移除<br>                   this.list.splice(i,j) &#x2F;&#x2F;移除从i开始的j个 <br>			&#125;<br>		&#125;<br>	&#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p><code>item</code>代表<code>list</code>中的值<code>index</code>代表编号</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>	&lt;div id&#x3D;&quot;app&quot;&gt;<br>		&lt;ul&gt;<br>			&lt;li v-for&#x3D;&quot;(item,index) in list&quot; :title&#x3D;&quot;item&quot;&gt;<br>				&#123;&#123;index&#125;&#125;--&#123;&#123;item&#125;&#125;<br>			&lt;&#x2F;li&gt;<br>		&lt;&#x2F;ul&gt;<br>		&lt;input type&#x3D;&quot;text&quot; @keyup.enter&#x3D;&quot;add(txt)&quot; v-model&#x3D;&quot;txt&quot; value&#x3D;&quot;txt&quot;&gt;回车添加<br>	&lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>	var app &#x3D; new Vue(&#123;<br>		el:&quot;#app&quot;,<br>		data:&#123;<br>			list :[1,2,3,4,5],<br>			txt:&quot;&quot;<br>		&#125;,<br>		methods:&#123;<br>			add:function(p1)&#123;<br>				this.list.push(p1);<br>				console.log(p1)<br>			&#125;<br>		&#125;<br>	&#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p>其中<code>p1</code>为形参可以接收传递进来的实参</p>
<p><code>v-model</code>可以实现双向绑定</p>
<h2 id="axios"><a href="#axios" class="headerlink" title="axios"></a>axios</h2><p>功能强大的网络请求库</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;script src=<span class="hljs-string">&quot;https://unpkg.com/axios/dist/axios.min.js&quot;</span>&gt;&lt;/script&gt;<br>axios.get(地址?查询字符串).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>)</span>&#123;&#125;,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err</span>)</span>&#123;&#125;);<br>axios.post(地址,&#123;<span class="hljs-attr">key</span>:value,<span class="hljs-attr">key2</span>:value2&#125;).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>)(<span class="hljs-params"></span>),<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err</span>)</span>&#123;&#125;)</span>;<br></code></pre></td></tr></table></figure>

<h3 id="天气查询小工具"><a href="#天气查询小工具" class="headerlink" title="天气查询小工具"></a>天气查询小工具</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>	&lt;div id&#x3D;&quot;app&quot;&gt;<br>		&lt;input type&#x3D;&quot;text&quot; @keyup.enter&#x3D;&quot;add(txt)&quot; v-model&#x3D;&quot;txt&quot; value&#x3D;&quot;txt&quot;&gt;回车查询<br>		&lt;ul&gt;<br>			&lt;li v-for&#x3D;&quot;item in list&quot; :title&#x3D;&quot;item&quot;&gt;<br>				&#123;&#123;item&#125;&#125;<br>			&lt;&#x2F;li&gt;<br>		&lt;&#x2F;ul&gt;<br>	&lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>	var app &#x3D; new Vue(&#123;<br>		el:&quot;#app&quot;,<br>		data:&#123;<br>			list :[],<br>			txt:&quot;&quot;<br>		&#125;,<br>		methods:&#123;<br>			add:function(p1)&#123;<br>				that &#x3D; this<br>				this.list &#x3D; [];<br>				axios.get(&quot;https:&#x2F;&#x2F;restapi.amap.com&#x2F;v3&#x2F;weather&#x2F;weatherInfo?key&#x3D;5d2d3e6c0d5188bec134fc4fc1b139e0&amp;city&#x3D;&quot; + this.txt +&quot;&amp;extensions&#x3D;base&quot;).then(function(response)&#123;<br>						var dateJson &#x3D; response.data.lives[0];<br>						console.log(response.data.lives[0]);<br>						that.list.push(&quot;时间:&quot; + dateJson[&#39;reporttime&#39;]);<br>						that.list.push(&quot;城市:&quot; + dateJson[&#39;province&#39;] + dateJson[&#39;city&#39;]);<br>						that.list.push(&quot;湿度:&quot; + dateJson[&#39;humidity&#39;]);<br>						that.list.push(&quot;气温:&quot; + dateJson[&#39;temperature&#39;]);<br>						that.list.push(&quot;天气:&quot; + dateJson[&#39;weather&#39;]);<br>						that.list.push(&quot;风向:&quot; + dateJson[&#39;winddirection&#39;]);<br>						that.list.push(&quot;风力:&quot; + dateJson[&#39;windpower&#39;]);<br>				&#125;,function(err)&#123;<br>					<br>				&#125;);<br>				<br>			&#125;<br>		&#125;<br>	&#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<h3 id="使用多个class"><a href="#使用多个class" class="headerlink" title="使用多个class"></a>使用多个class</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs html">   <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><br>       .c1&#123;<br><span class="css">           <span class="hljs-attribute">color</span>: red;</span><br>       &#125;<br>       .c2&#123;<br><span class="css">           <span class="hljs-attribute">background-color</span>: blue;</span><br>       &#125;<br>   <span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>    <br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span><br>       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">:class</span>=<span class="hljs-string">&quot;&#123;c1:true,c2:false&#125;&quot;</span>&gt;</span>今天的天气很好<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>在网页上应用了c1的class，c2没有应用</p>
<h4 id="computed-计算函数"><a href="#computed-计算函数" class="headerlink" title="computed 计算函数"></a>computed 计算函数</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs javascript">&lt;body&gt;<br>    &lt;div id=<span class="hljs-string">&quot;app&quot;</span>&gt;<br>        &lt;div&gt;&#123;&#123;aaaAndBbb&#125;&#125;&lt;/div&gt;<br>    &lt;/div&gt;<br>&lt;/body&gt;<br>&lt;script&gt;<br>    <span class="hljs-keyword">const</span> vm = <span class="hljs-keyword">new</span> Vue(&#123;<br>        el:<span class="hljs-string">&quot;#app&quot;</span>,<br>        data:&#123;<br>            a:<span class="hljs-string">&#x27;aaa&#x27;</span>,<br>            b:<span class="hljs-string">&#x27;bbb&#x27;</span><br>        &#125;,<br>        computed:&#123;<br>            aaaAndBbb:<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)</span>&#123;<br>                <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.a + <span class="hljs-built_in">this</span>.b;<br>            &#125;<br>        &#125;<br>    &#125;)<br>&lt;/script&gt;<br></code></pre></td></tr></table></figure>

<p>直接调用<code>aaaAndBbb</code>就可以返回<code>aaabbb</code></p>
<h4 id="ES6增强写法"><a href="#ES6增强写法" class="headerlink" title="ES6增强写法"></a>ES6增强写法</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span>  a = &#123;<br>    <span class="hljs-function"><span class="hljs-title">aaa</span>(<span class="hljs-params"></span>)</span>&#123;<br>        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;aaa&quot;</span>);<br>    &#125;,<br>    <span class="hljs-function"><span class="hljs-title">bbb</span>(<span class="hljs-params"></span>)</span>&#123;<br>        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;bbb&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h4 id="调用event变量"><a href="#调用event变量" class="headerlink" title="调用event变量"></a>调用event变量</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs plain">&lt;body&gt;<br>    &lt;div id&#x3D;&quot;app&quot;&gt;<br>        &lt;button @click&#x3D;&quot;clickGo(&#39;123&#39;,$event)&quot;&gt;123456&lt;&#x2F;button&gt;<br>    &lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>    const vm &#x3D; new Vue(&#123;<br>        el:&quot;#app&quot;,<br>        data:&#123;<br><br>        &#125;,<br>        methods:&#123;<br>            clickGo:function (a,b)&#123;<br>                console.log(a, b);<br>            &#125;<br>        &#125;<br>    &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<h4 id="sopt-阻止冒泡"><a href="#sopt-阻止冒泡" class="headerlink" title="sopt 阻止冒泡"></a>sopt 阻止冒泡</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>    &lt;div id&#x3D;&quot;app&quot;&gt;<br>        &lt;div @click&#x3D;&quot;divClick&quot;&gt;<br>            &lt;button @click.stop&#x3D;&quot;butClick&quot;&gt;点我&lt;&#x2F;button&gt;<br>        &lt;&#x2F;div&gt;<br>    &lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>    const vm &#x3D; new Vue(&#123;<br>        el:&quot;#app&quot;,<br>        data:&#123;<br><br>        &#125;,<br>        methods:&#123;<br>            divClick:function ()&#123;<br>                console.log(&quot;div事件冒泡&quot;);<br>            &#125;,<br>            butClick:function ()&#123;<br>                console.log(&quot;按钮事件冒泡,我阻止了div事件冒泡&quot;);<br>            &#125;<br>        &#125;<br>    &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p>点击按钮，只有按钮事件被执行</p>
<h4 id="prevent-阻止默认事件"><a href="#prevent-阻止默认事件" class="headerlink" title="prevent 阻止默认事件"></a>prevent 阻止默认事件</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>    &lt;div id&#x3D;&quot;app&quot;&gt;<br>            &lt;form action&#x3D;&quot;#&quot;&gt;<br>                &lt;input type&#x3D;&quot;submit&quot; value&#x3D;&quot;提交&quot; @click.prevent&#x3D;&quot;butClick&quot;&gt;<br>            &lt;&#x2F;form&gt;<br>    &lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>    const vm &#x3D; new Vue(&#123;<br>        el:&quot;#app&quot;,<br>        data:&#123;<br><br>        &#125;,<br>        methods:&#123;<br>            butClick:function ()&#123;<br>                console.log(&quot;按钮事件冒泡,我阻止了原来的提交事件&quot;);<br>            &#125;<br>        &#125;<br>    &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p>点击提交后<code>&lt;input&gt;</code>默认的提交事件会被阻止</p>
<h4 id="一些数组方法"><a href="#一些数组方法" class="headerlink" title="一些数组方法"></a>一些数组方法</h4><h5 id="push-在最后添加一个元素"><a href="#push-在最后添加一个元素" class="headerlink" title="push 在最后添加一个元素"></a>push 在最后添加一个元素</h5><h5 id="pop-删除数组最后一个元素"><a href="#pop-删除数组最后一个元素" class="headerlink" title="pop 删除数组最后一个元素"></a>pop 删除数组最后一个元素</h5><h5 id="shift-删除数组中的第一个元素"><a href="#shift-删除数组中的第一个元素" class="headerlink" title="shift 删除数组中的第一个元素"></a>shift 删除数组中的第一个元素</h5><h5 id="unshift-在数组的最前面添加元素"><a href="#unshift-在数组的最前面添加元素" class="headerlink" title="unshift 在数组的最前面添加元素"></a>unshift 在数组的最前面添加元素</h5><h5 id="splice-在某个位置插入元素"><a href="#splice-在某个位置插入元素" class="headerlink" title="splice 在某个位置插入元素"></a>splice 在某个位置插入元素</h5><h4 id="JavaScript-可变参数-…"><a href="#JavaScript-可变参数-…" class="headerlink" title="JavaScript 可变参数 …"></a>JavaScript 可变参数 …</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">aaa</span>(<span class="hljs-params">...num</span>)</span>&#123;<br>    <span class="hljs-built_in">console</span>.log(num);<br>&#125;<br></code></pre></td></tr></table></figure>

<p>该函数可接受任意个参数。</p>
<h4 id="点击哪个哪个变红"><a href="#点击哪个哪个变红" class="headerlink" title="点击哪个哪个变红"></a>点击哪个哪个变红</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>    &lt;div id&#x3D;&quot;app&quot;&gt;<br>        &lt;ul&gt;<br>            &lt;li v-for&#x3D;&quot;(item,index) in a&quot; :class&#x3D;&quot;&#123;rrr:isRed &#x3D;&#x3D; index&#125;&quot; v-on:click&#x3D;&quot;toRed(index)&quot;&gt;&#123;&#123;index + &quot; . &quot; + item&#125;&#125;&lt;&#x2F;li&gt;<br>        &lt;&#x2F;ul&gt;<br>    &lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>    const vm &#x3D; new Vue(&#123;<br>        el:&quot;#app&quot;,<br>        data:&#123;<br>            a:[&quot;aaa&quot;,&quot;bbb&quot;,&quot;ccc&quot;,&quot;ddd&quot;],<br>            isRed:-1<br>        &#125;,<br>        methods:&#123;<br>            toRed:function (a)&#123;<br>                this.isRed &#x3D; a;<br>            &#125;<br>        &#125;<br>    &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>



<h4 id="key"><a href="#key" class="headerlink" title="key"></a>key</h4><h4 id="splice"><a href="#splice" class="headerlink" title="splice"></a>splice</h4><p>插入替换元素</p>
<h3 id="过滤器"><a href="#过滤器" class="headerlink" title="过滤器"></a>过滤器</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;div id&#x3D;&quot;app&quot;&gt;<br>  &#123;&#123;jg_one  | addOne&#125;&#125;<br>&lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>  const vm &#x3D; new Vue(&#123;<br>    el:&quot;#app&quot;,<br>    data:&#123;<br>      jg_one:50<br>    &#125;,<br>    filters:&#123;<br>      addOne(jg)&#123;<br>        return jg + 100;<br>      &#125;<br>    &#125;<br>  &#125;)<br></code></pre></td></tr></table></figure>

<p>在<code>filters</code>中定义过滤器，在变量后，使用<code>|</code>来使用过滤器，过滤器自动把前面的变量当做参数传入</p>
<h4 id="in"><a href="#in" class="headerlink" title="in"></a>in</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>&lt;div id&#x3D;&quot;app&quot;&gt;<br>  &lt;ul v-for&#x3D;&quot;i in t&quot;&gt;<br>    &lt;li&gt;&#123;&#123;i&#125;&#125;&lt;&#x2F;li&gt;<br>  &lt;&#x2F;ul&gt;<br>&lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>  const vm &#x3D; new Vue(&#123;<br>    el:&quot;#app&quot;,<br>    data:&#123;<br>      t:[&quot;aaa&quot;,&quot;bbb&quot;,&quot;ccc&quot;]<br>    &#125;<br>  &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p>使用<code>in</code>来遍历<code>t</code>列表</p>
<h4 id="filter"><a href="#filter" class="headerlink" title="filter()"></a>filter()</h4><p><code>list.filter()</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">let</span> a = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>];<br>    <span class="hljs-keyword">let</span> b = a.filter(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">n</span>)</span>&#123;<br>      <span class="hljs-keyword">return</span> n &gt; <span class="hljs-number">3</span><br>    &#125;)<br><span class="hljs-built_in">console</span>.log(b)<br></code></pre></td></tr></table></figure>

<p><code>filter</code>类似于过滤器，本示例中输出 <code>[4,5,6]</code></p>
<h4 id="map"><a href="#map" class="headerlink" title="map()"></a>map()</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">let</span> a = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>];<br><span class="hljs-keyword">let</span> b = a.map(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">n</span>)</span>&#123;<br>  <span class="hljs-keyword">return</span> n * <span class="hljs-number">2</span><br>&#125;)<br><span class="hljs-built_in">console</span>.log(b)<br></code></pre></td></tr></table></figure>

<p><code>map</code>返回函数中操作的结果，本示例中输出<code>[2, 4, 6, 8, 10, 12]</code></p>
<h4 id="reduce"><a href="#reduce" class="headerlink" title="reduce()"></a>reduce()</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">let</span> a = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>];<br><span class="hljs-keyword">let</span> b = a.reduce(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">pre,n</span>)</span>&#123;<br>  <span class="hljs-keyword">return</span>  pre += n<br>&#125;,<span class="hljs-number">0</span>)<br><span class="hljs-built_in">console</span>.log(b)<br></code></pre></td></tr></table></figure>

<p><code>reduce</code>有三个参数，<code>reduce(function(初始数,正在取出的数),初始数的初始值)</code></p>
<p>本示例中输出<code>21</code></p>
<h4 id="以上三个函数均可以用箭头函数来执行"><a href="#以上三个函数均可以用箭头函数来执行" class="headerlink" title="以上三个函数均可以用箭头函数来执行"></a>以上三个函数均可以用箭头函数来执行</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">let</span> a = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>];<br><span class="hljs-keyword">let</span> b = a.reduce(<span class="hljs-function">(<span class="hljs-params">pre,n</span>) =&gt;</span> pie + n)<br><span class="hljs-built_in">console</span>.log(b)<br></code></pre></td></tr></table></figure>



<h4 id="v-model在checkbox中的使用"><a href="#v-model在checkbox中的使用" class="headerlink" title="v-model在checkbox中的使用"></a>v-model在checkbox中的使用</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs plain">&lt;body&gt;<br>&lt;div id&#x3D;&quot;app&quot;&gt;<br>    &lt;input type&#x3D;&quot;checkbox&quot; value&#x3D;&quot;111&quot; v-model&#x3D;&quot;a&quot;&gt;111<br>    &lt;input type&#x3D;&quot;checkbox&quot; value&#x3D;&quot;222&quot; v-model&#x3D;&quot;a&quot;&gt;222<br>    &lt;input type&#x3D;&quot;checkbox&quot; value&#x3D;&quot;333&quot; v-model&#x3D;&quot;a&quot;&gt;333<br>    &#123;&#123;a&#125;&#125;<br>&lt;&#x2F;div&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>  const vm &#x3D; new Vue(&#123;<br>    el:&quot;#app&quot;,<br>    data:&#123;<br>      a:[]<br>    &#125;<br>  &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p>在选中复选框后，<code>&#123;&#123;a&#125;&#125;</code>处会实时渲染出a数组本示例全选中后显示的是：<code> [ &quot;111&quot;, &quot;222&quot;, &quot;333&quot; ]</code></p>
<h4 id="v-model的一些操作"><a href="#v-model的一些操作" class="headerlink" title="v-model的一些操作"></a>v-model的一些操作</h4><h5 id="v-model-lazy"><a href="#v-model-lazy" class="headerlink" title="v-model.lazy"></a>v-model.lazy</h5><p>用于在鼠标失去焦点或者按下回车的时候同步值</p>
<h5 id="v-model-number"><a href="#v-model-number" class="headerlink" title="v-model.number"></a>v-model.number</h5><p>用于把用户输入的数字转换为数字形式</p>
<h5 id="v-model-trim"><a href="#v-model-trim" class="headerlink" title="v-model.trim"></a>v-model.trim</h5><p>用于将用户输入的内容两边的空格去除</p>
<h4 id="组件"><a href="#组件" class="headerlink" title="组件"></a>组件</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>&lt;div id&#x3D;&quot;app&quot;&gt;<br>    &lt;mya&gt;&lt;&#x2F;mya&gt;<br>&lt;&#x2F;div&gt;<br>&lt;script&gt;<br>    &#x2F;&#x2F; 注册<br>    Vue.component(&#39;mya&#39;, &#123;<br>        template: &#39;&lt;h1&gt;自定义组件!&lt;&#x2F;h1&gt;&#39;<br>    &#125;)<br>    <br>    &#x2F;&#x2F; 创建根实例<br>    new Vue(&#123;<br>        el: &#39;#app&#39;<br>    &#125;)<br>&lt;&#x2F;script&gt;<br>&lt;&#x2F;body&gt;<br></code></pre></td></tr></table></figure>



<h4 id="局部组件和全局组件"><a href="#局部组件和全局组件" class="headerlink" title="局部组件和全局组件"></a>局部组件和全局组件</h4><h5 id="局部组件"><a href="#局部组件" class="headerlink" title="局部组件"></a>局部组件</h5><p>只可以在声明的vue中使用</p>
<h5 id="全局组件"><a href="#全局组件" class="headerlink" title="全局组件"></a>全局组件</h5><p>声明了之后，可以在任意位置使用</p>
<h4 id="父子组件的通信"><a href="#父子组件的通信" class="headerlink" title="父子组件的通信"></a>父子组件的通信</h4><ul>
<li>注意子组件中的data必须是一个函数</li>
</ul>
<h5 id="props"><a href="#props" class="headerlink" title="props"></a>props</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>  &lt;div id&#x3D;&quot;app&quot;&gt;<br>    &lt;cpn v-bind:cmsg&#x3D;&quot;msg&quot;&gt;&lt;&#x2F;cpn&gt;<br>  &lt;&#x2F;div&gt;<br><br>  &lt;template id&#x3D;&quot;mb&quot;&gt;<br>      &lt;div&gt;&#123;&#123;cmsg&#125;&#125;&lt;&#x2F;div&gt;<br>  &lt;&#x2F;template&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br><br>  const mb &#x3D; &#123;<br>    template:&quot;#mb&quot;,<br>    props:[&quot;cmsg&quot;]<br>  &#125;<br><br> const vm &#x3D; new Vue(&#123;<br>   el:&quot;#app&quot;,<br>   data:&#123;<br>     msg:&quot;Hello&quot;<br>   &#125;,<br>   components:&#123;<br>      cpn:mb<br>   &#125;<br> &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p>如代码中所示，在父组件vue中定义了一个<code>msg</code>，又定义了一个子组件 <code>mb</code> ，我们的目的是在<code>app</code>节点之外，访问到<code>msg</code>这个变量的值，首先我们要做的是在根节点中使用<code>cpn:mb</code>绑定这个模板<code>mb</code>，然后在模板对象中使用<code>props:[&#39;cmsg&#39;]</code>    设置父节点映射到子节点中的变量名。然后，在根节点中创建一个模板<code>&lt;cpn&gt;</code>，使用 <code>v-bind:cmsg=:&quot;msg&quot;</code> 来绑定子节点中的<code>cmsg</code>和父节点中的<code>msg</code>，这样就可以在子节点中访问到父节点中的内容了。</p>
<h5 id="类型限定"><a href="#类型限定" class="headerlink" title="类型限定"></a>类型限定</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs vue">props:&#123;<br>    cmsg:String<br>&#125;<br>&#x2F;&#x2F;或者这样<br>props:&#123;<br>      cmsg:&#123;<br>          type:String,<br>          default:&#39;123&#39;, &#x2F;&#x2F;默认值,类型是对象或者数组时，对象要是一个函数<br>          required:true &#x2F;&#x2F;使用时必传该参数<br>      &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h5 id="子传父"><a href="#子传父" class="headerlink" title="子传父"></a>子传父</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>  &lt;div id&#x3D;&quot;app&quot;&gt;<br>    &lt;cpn @btnclick&#x3D;&quot;cpnClick&quot;&gt;&lt;&#x2F;cpn&gt;<br>  &lt;&#x2F;div&gt;<br><br>  &lt;template id&#x3D;&quot;mb&quot;&gt;<br>    &lt;button @click&#x3D;&quot;btnClick(msg)&quot;&gt;&#123;&#123;msg&#125;&#125;&lt;&#x2F;button&gt;<br>  &lt;&#x2F;template&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>  const mb &#x3D; &#123;<br>    template:&quot;#mb&quot;,<br>    data()&#123;<br>      return&#123;<br>        msg:&quot;Hello&quot;<br>      &#125;<br>    &#125;,<br>    methods:&#123;<br>      btnClick:function (msg)&#123;<br>        this.$emit(&#39;btnclick&#39;,msg)<br>      &#125;<br>    &#125;<br>  &#125;<br><br> const vm &#x3D; new Vue(&#123;<br>   el:&quot;#app&quot;,<br>   data:&#123;<br>   &#125;,<br>   methods: &#123;<br>     cpnClick:function (msg)&#123;<br>        console.log(msg)<br>     &#125;<br>   &#125;,<br>   components:&#123;<br>      cpn:mb<br>   &#125;<br> &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p>我们的目的是把子组件中的<code>msg</code>传给父组件，首先，我们要在子组件中定义一个方法<code>btnClick</code>其中<code>this.$emit(&#39;btnclick&#39;,msg)</code>用来传递参数，<code>this.$emit(&#39;btnclick&#39;)</code>用于把<code>event</code>对象发送到父组件中，后面跟着的<code>msg</code>表示只发送msg参数，在父组件中，我们要创建一个<code>cpnClick</code>函数来接收参数，接下来，我们要用<code>&lt;cpn @btnclick=&quot;cpnClick&quot;&gt;&lt;/cpn&gt;</code>把<code>btnclick</code>和<code>cpnClick</code>绑定在一起。</p>
<p>当子组件中的按钮被点击时，调用子组件中的<code>btnClick</code>函数，该函数使用<code>$emit</code>通过绑定的函数把数据传送到父节点。</p>
<h4 id="父访问子"><a href="#父访问子" class="headerlink" title="父访问子"></a>父访问子</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs plain">&lt;body&gt;<br>  &lt;div id&#x3D;&quot;app&quot;&gt;<br>    &lt;button @click&#x3D;&quot;btnClick&quot;&gt;Click&lt;&#x2F;button&gt;<br>    &lt;cpn1&gt;&lt;&#x2F;cpn1&gt;<br>  &lt;&#x2F;div&gt;<br><br>&lt;template id&#x3D;&quot;mb1&quot;&gt;&lt;&#x2F;template&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>  const vm &#x3D; new Vue(&#123;<br>    el:&quot;#app&quot;,<br>    methods:&#123;<br>      btnClick()&#123;<br>        console.log(this.$children);<br>      &#125;<br>    &#125;,<br>    components  :&#123;<br>      &#39;cpn1&#39;:&#123;<br>        template:&quot;#mb1&quot;,<br>      &#125;    &#125;<br>  &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>





<h4 id="slot"><a href="#slot" class="headerlink" title="slot"></a>slot</h4><p>Vue插槽</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;div id&#x3D;&quot;app&quot;&gt;<br>  &lt;cpn1&gt;<br>    &lt;div&gt;你好&lt;&#x2F;div&gt;<br>  &lt;&#x2F;cpn1&gt;<br>&lt;&#x2F;div&gt;<br><br>&lt;template id&#x3D;&quot;mb1&quot;&gt;<br>  &lt;div&gt;<br>    hello<br>    &lt;slot&gt;&lt;&#x2F;slot&gt;<br>  &lt;&#x2F;div&gt;<br>&lt;&#x2F;template&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>  const vm &#x3D; new Vue(&#123;<br>    el:&quot;#app&quot;,<br>    components:&#123;<br>      &#39;cpn1&#39;:&#123;<br>        template:&quot;#mb1&quot;<br>      &#125;<br>    &#125;<br>  &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p>使用<code>&lt;slot&gt;</code>来定义插槽</p>
<p>插槽类似于接口，在模板定义了里面的内容之后，使用<code>&lt;slot&gt;</code>来放置一个插槽接口，这操作后，我们就可以在使用这个模板的时候，使用类似于<code>&lt;div&gt;你好&lt;/div&gt;</code>的操作往模板里放进任意元素。</p>
<h4 id="具名插槽使用"><a href="#具名插槽使用" class="headerlink" title="具名插槽使用"></a>具名插槽使用</h4><p>在使用插槽的时候，如果我们定义了多个插槽，那么在使用的时候，在一个插槽中放入东西，所有插槽就会跟着改变，所以，我们可以给插槽定义一个名字</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs vue">	&lt;div id&#x3D;&quot;app&quot;&gt;<br>  &lt;cpn1&gt;<br>    &lt;div slot&#x3D;&quot;a1&quot;&gt;你好1&lt;&#x2F;div&gt; &lt;!--使用a1插槽--&gt;<br>  &lt;&#x2F;cpn1&gt;<br>  &lt;cpn1&gt;<br>    &lt;div slot&#x3D;&quot;a2&quot;&gt;你好2&lt;&#x2F;div&gt;&lt;!--使用a2插槽--&gt;<br>  &lt;&#x2F;cpn1&gt;<br>  &lt;cpn1&gt;<br>    &lt;div slot&#x3D;&quot;a3&quot;&gt;你好3&lt;&#x2F;div&gt;&lt;!--使用a3插槽--&gt;<br>  &lt;&#x2F;cpn1&gt;<br>&lt;&#x2F;div&gt;<br><br>&lt;template id&#x3D;&quot;mb1&quot;&gt;<br>  &lt;div&gt;<br>    &lt;slot name&#x3D;&quot;a1&quot;&gt;&lt;&#x2F;slot&gt;&lt;!--定义a1插槽--&gt;<br>    &lt;slot name&#x3D;&quot;a2&quot;&gt;&lt;&#x2F;slot&gt;&lt;!--定义a2插槽--&gt;<br>    &lt;slot name&#x3D;&quot;a3&quot;&gt;&lt;&#x2F;slot&gt;&lt;!--定义a3插槽--&gt;<br>  &lt;&#x2F;div&gt;<br>&lt;&#x2F;template&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>  const vm &#x3D; new Vue(&#123;<br>    el:&quot;#app&quot;,<br>    components:&#123;<br>      &#39;cpn1&#39;:&#123;<br>        template:&quot;#mb1&quot;<br>      &#125;<br>    &#125;<br>  &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>



<h4 id="编译作用域"><a href="#编译作用域" class="headerlink" title="编译作用域"></a>编译作用域</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>&lt;div id&#x3D;&quot;app&quot;&gt;<br>  &lt;cpn1&gt;<br>    &lt;div v-show&#x3D;&quot;isShow&quot;&gt;你好&lt;&#x2F;div&gt;<br>  &lt;&#x2F;cpn1&gt;<br>&lt;&#x2F;div&gt;<br><br>&lt;template id&#x3D;&quot;mb1&quot;&gt;<br>  &lt;div&gt;<br>    &lt;slot&gt;&lt;&#x2F;slot&gt;<br>    &lt;div v-show&#x3D;&quot;isShow&quot;&gt;今天的天气很好&lt;&#x2F;div&gt;<br>  &lt;&#x2F;div&gt;<br>&lt;&#x2F;template&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>  const vm &#x3D; new Vue(&#123;<br>    el:&quot;#app&quot;,<br>    data:&#123;<br>      isShow:true<br>    &#125;,<br>    components:&#123;<br>      &#39;cpn1&#39;:&#123;<br>        template:&quot;#mb1&quot;,<br>        data()&#123;<br>          return&#123;<br>            isShow:false<br>          &#125;<br>        &#125;<br>      &#125;<br>    &#125;<br>  &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>

<p>如本示例：</p>
<p>我们在模板中定义了一个<code>div</code>他的<code>v-show=isSwho</code>，我们在使用该模板的时候，在插槽中定义了一个<code>div</code>他的<code>v-show=isShow</code>，但是在浏览器中渲染的时候，<code>今天天气很好</code>不会被渲染出来，<code>你好</code>会被渲染出来，这是因为，在模板中定义的<code>isShow</code>使用的是模板中定义的值，而在根节点<code>app</code>中定义的<code>isSwho</code>使用的是根节点<code>data</code>中的值。</p>
<p><strong>插槽总结：父组件替换插槽标签，但是内容由子组件来提供</strong></p>
<p><strong>需要在多个界面进行展示</strong></p>
<p><strong>内容在子组件，希望父组件告诉我们如何展示</strong></p>
<h4 id="在插槽中使用组件中定义的变量"><a href="#在插槽中使用组件中定义的变量" class="headerlink" title="在插槽中使用组件中定义的变量"></a>在插槽中使用组件中定义的变量</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;body&gt;<br>    &lt;div id&#x3D;&quot;app&quot;&gt;<br>        &lt;cpn1&gt;&lt;&#x2F;cpn1&gt;<br>        &lt;cpn1&gt;<br>            &lt;template slot-scope&#x3D;&quot;slot&quot;&gt;<br>                &lt;ul&gt;<br>                    &lt;li v-for&#x3D;&quot;item in slot.data&quot;&gt;&#123;&#123;item&#125;&#125;&lt;&#x2F;li&gt;<br>                &lt;&#x2F;ul&gt;<br>            &lt;&#x2F;template&gt;<br>        &lt;&#x2F;cpn1&gt;<br>    &lt;&#x2F;div&gt;<br>    &lt;template id&#x3D;&quot;mb1&quot;&gt;<br>        &lt;slot :data&#x3D;&quot;Name&quot;&gt;&lt;&#x2F;slot&gt;<br>        &lt;div&gt;hello&lt;&#x2F;div&gt;<br>    &lt;&#x2F;template&gt;<br>&lt;&#x2F;body&gt;<br>&lt;script&gt;<br>  const vm &#x3D; new Vue(&#123;<br>    el:&quot;#app&quot;,<br>    data:&#123;<br>    &#125;,<br>   components:&#123;<br>        &quot;cpn1&quot;:&#123;<br>            template:&quot;#mb1&quot;,<br>            data()&#123;<br>                return&#123;<br>                    &quot;Name&quot;:[&quot;111&quot;,&quot;222&quot;,&quot;333&quot;]<br>                &#125;<br>            &#125;<br>        &#125;<br>    &#125;<br>  &#125;)<br>&lt;&#x2F;script&gt;<br></code></pre></td></tr></table></figure>



<p>如代码所示：我们在子组件中第定义了一个变量<code>Name</code>，并且子组件中有一个插槽，我们的目的是在自定义插槽中的内容的时候，可以调用在组件中定义的变量。</p>
<p>首先在定义插槽的<code>slot</code>标签中，使用<code>:data=&quot;Name&quot;</code>来创建一个<code>data</code>变量，这里的<code>data</code>可以是任意名称。</p>
<p>接下来我们在使用的时候</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;cpn1&gt;<br>    &lt;template slot-scope&#x3D;&quot;slot&quot;&gt;<br>        &lt;ul&gt;<br>            &lt;li v-for&#x3D;&quot;item in slot.data&quot;&gt;&#123;&#123;item&#125;&#125;&lt;&#x2F;li&gt;<br>        &lt;&#x2F;ul&gt;<br>    &lt;&#x2F;template&gt;<br>&lt;&#x2F;cpn1&gt;<br></code></pre></td></tr></table></figure>



<p>要先在模板插槽中定义一个 <code>template</code>或其他的节点，<code>slot-scope=&quot;slot&quot;</code>使用<code>slot</code>或其他变量名来定义接收到的变量，然后使用<code>slot.data</code>来调用变量</p>

      
      <!-- reward -->
      
      <div id="reward-btn">
        打赏
      </div>
      
    </div>
    
    
      <!-- copyright -->
      
        <div class="declare">
          <ul class="post-copyright">
            <li>
              <i class="ri-copyright-line"></i>
              <strong>版权声明： </strong s>
              本博客所有文章除特别声明外，均采用 <a href="https://www.apache.org/licenses/LICENSE-2.0.html" rel="external nofollow"
                target="_blank">Apache License 2.0</a> 许可协议。转载请注明出处！
            </li>
          </ul>
        </div>
        
    <footer class="article-footer">
      
          
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://zhfhz.gitee.io/2021/Vue%E7%AC%94%E8%AE%B0/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E7%BC%96%E7%A8%8B/" rel="tag">编程</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2021/Python-%E5%9F%BA%E7%A1%80/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Node.js笔记
          
        </div>
      </a>
    
    
      <a href="/2021/%E7%95%AA%E8%8C%84%E6%97%B6%E9%97%B4%E8%A7%84%E5%88%92%E6%B3%95/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">番茄时间规划法</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        app_id: 'QExPaN3yEdYGXDLijnPKdTc8-MdYXbMMI',
        app_key: 'kyQzz6yRJkdRHcMm0tpOvSYY',
        path: window.location.pathname,
        notify: 'true',
        verify: 'false',
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020-2021
        ZHF
      </li>
      <li>
        
        Powered by
        
        
        <a href="https://hexo.io" target="_blank">Hexo</a> Theme <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src=''></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
    <aside class="sidebar">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="岛"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E6%97%85%E8%A1%8C/">旅行</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="https://500px.me/ZHFHZ">摄影</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/share.js"></script>


<script src="/js/lazyload.min.js"></script>


<script>
  try {
    var typed = new Typed("#subtitle", {
      strings: ['面朝大海，春暖花开', '愿你一生努力，一生被爱', '想要的都拥有，得不到的都释怀'],
      startDelay: 0,
      typeSpeed: 200,
      loop: true,
      backSpeed: 100,
      showCursor: true
    });
  } catch (err) {
  }

</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>



<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/js/ayer.js"></script>



<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>




<script src="/js/busuanzi-2.3.pure.min.js"></script>



<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>

    
  </div>
</body>

</html>